package com.gky.gkytemplate.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gky.gkytemplate.common.constant.UserConstant;
import com.gky.gkytemplate.common.context.BaseContext;
import com.gky.gkytemplate.common.enums.UserRoleEnum;
import com.gky.gkytemplate.pojo.entity.User;
import com.gky.gkytemplate.pojo.vo.UserLoginVo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;

@SpringBootTest
public class testRedis {


    @Resource
    private RedisTemplate redisTemplate;


    @Test
    public void testRedisTemplate() {
        User user = new User();
        user.setId(1L);
        user.setUserName("gky");
        redisTemplate.opsForValue().set(UserConstant.USER_LOGIN + user.getId(), user);
    }

    @Test
    public void testStringRedisTemplate() {
        UserLoginVo o = (UserLoginVo) redisTemplate.opsForValue().get("user_login_1");
//        JSONObject jsonObject = JSON.parseObject(o);
//        UserLoginVo user = JSON.parseObject(o, UserLoginVo.class);
        System.out.println(o.getToken());

        //登录用户所拥有的角色
        UserLoginVo user = (UserLoginVo) redisTemplate.opsForValue().get(UserConstant.USER_LOGIN + "_" + BaseContext.getCurrentId());
        UserRoleEnum userRoleEnum = UserRoleEnum.getEnumsByValue(user.getUserRole());
        System.out.println(userRoleEnum);
    }

}
